import {message} from "@/hooks/modules/useMessage";

export const hexToRgb = (str: string) => {
    let hexs: any = "";
    const reg = /^#?[0-9A-Fa-f]{6}$/;
    if (!reg.test(str)) return message.warning("传入的hex值格式有误！");
    str = str.replace("#", "");
    hexs = str.match(/../g);
    for (let i = 0; i < 3; i++) hexs[i] = parseInt(hexs[i], 16);
    return hexs;
}

export const rgbToHex = (r: string, g: string, b: string) => {
    const reg = /^\d{1,3}$/;
    if (!reg.test(r) || !reg.test(g) || !reg.test(b)) return message.warning("传入的rgb值格式有误！");
    // @ts-ignore
    const hexes = [r.toString(16), g.toString(16), b.toString(16)];
    for (let i = 0; i < 3; i++) if (hexes[i].length == 1) hexes[i] = `0${hexes[i]}`;
    return `#${hexes.join("")}`;
}

export const getDarkColor = (color: string, level: number) => {
    const reg = /^#?[0-9A-Fa-f]{6}$/;
    if (!reg.test(color)) return message.warning("传入的hex值格式有误！");
    const rgb = hexToRgb(color);
    for (let i = 0; i < 3; i++) rgb[i] = Math.round(20.5 * level + rgb[i] * (1 - level));
    return rgbToHex(rgb[0], rgb[1], rgb[2]);
}

export const getLightColor = (color: string, level: number) => {
    const reg = /^#?[0-9A-Fa-f]{6}$/;
    if (!reg.test(color)) message.warning("传入的hex值格式有误！");
    const rgb = hexToRgb(color);
    for (let i = 0; i < 3; i++) rgb[i] = Math.round(255 * level + rgb[i] * (1 - level));
    return rgbToHex(rgb[0], rgb[1], rgb[2]);
}
